//
//  KKLiveMessageBaseTableViewCell.m
//  KKLiveDemo
//
//  Created by leo on 2017/7/30.
//  Copyright © 2017年 leo. All rights reserved.
//

#import "KKLiveMessageBaseTableViewCell.h"
#import "KKMessageItem+UITableViewCell.h"

@implementation KKLiveMessageBaseTableViewCell

- (instancetype)initWithC2CReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier: reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        _cellStyle = KKLiveMessageBaseTableViewCellStyle_C2C;
        [self addC2CCellViews];
    }
    return self;
}
- (instancetype)initWithGroupReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier: reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        _cellStyle = KKLiveMessageBaseTableViewCellStyle_Group;
        [self addGroupCellViews];
    }
    return self;
}
#pragma mark - Override methods
- (void)layoutSubviews {
    [super layoutSubviews];
    [self relayoutFrameOfSubViews];
}

#pragma mark - Private methods
- (void)relayoutFrameOfSubViews {
    switch (_cellStyle) {
        case KKLiveMessageBaseTableViewCellStyle_C2C:
        {
            [self relayoutC2CCellViews];
        }
            break;
        case KKLiveMessageBaseTableViewCellStyle_Group:
        {
            [self relayoutGroupCellViews];
        }
            break;
            
        default:
            break;
    }
}

- (void)relayoutC2CCellViews {
    //  重新布局C2C类型的cell
    CGRect rect = self.contentView.bounds;
    
    CGSize contentBackSize = [self.msg contentBackSizeInWidth:rect.size.width];
    CGSize contentSize = [self.msg contentSizeInWidth:rect.size.width];
    
    _msgContent.backgroundColor = [UIColor blueColor];
    _btn_icon.backgroundColor = [UIColor redColor];
    _img_contentBackground.backgroundColor = [UIColor orangeColor];
    
    if (self.msg.isMineMsg) {
        //  自己发送的消息
        _btn_icon.frame = CGRectMake(10, 10, 36, 36);
        
        _img_contentBackground.frame = CGRectMake(CGRectGetMaxX(_btn_icon.frame) + 5, 10, contentBackSize.width, contentBackSize.height);
        
        _msgContent.frame = CGRectMake(5, 5, contentSize.width, contentSize.height);
    }
    else {
        //  接收到的消息
        _btn_icon.frame = CGRectMake(CGRectGetWidth(rect) - 46, 10, 36, 36);
        
        _img_contentBackground.frame = CGRectMake(CGRectGetMinX(_btn_icon.frame) - 5 - contentBackSize.width, 10, contentBackSize.width, contentBackSize.height);
        
        _msgContent.frame = CGRectMake(5, 5, contentSize.width, contentSize.height);
    }
}

- (void)relayoutGroupCellViews {
    //  重新布局group类型的cell
    
    CGRect rect = self.contentView.bounds;
    
    CGSize contentBackSize = [self.msg contentBackSizeInWidth:rect.size.width];
    CGSize contentSize = [self.msg contentSizeInWidth:rect.size.width];
    
    _msgContent.backgroundColor = [UIColor blueColor];
    _btn_icon.backgroundColor = [UIColor redColor];
    _img_contentBackground.backgroundColor = [UIColor orangeColor];
    
    if (self.msg.isMineMsg) {
        //  自己发送的消息
        
        _lab_remarkTips.frame = CGRectMake(10, 5, 120, 10);
        _lab_remarkTips.text = @"群成员备注信息";
        
        _lab_nickName.frame = CGRectMake(10, 15, 150, 15);
        _lab_nickName.text = @"群组昵称";
        
        _btn_icon.frame = CGRectMake(10, 30, 36, 36);
        
        _img_contentBackground.frame = CGRectMake(CGRectGetMaxX(_btn_icon.frame) + 5, CGRectGetMinY(_btn_icon.frame), contentBackSize.width, contentBackSize.height);
        
        _msgContent.frame = CGRectMake(5, 5, contentSize.width, contentSize.height);
    }
    else {
        //  接收到的消息
        _btn_icon.frame = CGRectMake(CGRectGetWidth(rect) - 46, 10, 36, 36);
        
        _img_contentBackground.frame = CGRectMake(CGRectGetMinX(_btn_icon.frame) - 5 - contentBackSize.width, CGRectGetMinY(_btn_icon.frame), contentBackSize.width, contentBackSize.height);
        
        _msgContent.frame = CGRectMake(5, 5, contentSize.width, contentSize.height);
    }
}

#pragma mark - Public methods
// 添加C2C样式下的控件
- (void)addC2CCellViews {
    _btn_icon = [UIButton buttonWithType: UIButtonTypeCustom];
    _btn_icon.layer.cornerRadius = 18.0;
    _btn_icon.layer.masksToBounds = true;
    [self.contentView addSubview: _btn_icon];
    
    _img_contentBackground = [[UIImageView alloc] init];
    _img_contentBackground.userInteractionEnabled = YES;
    [self.contentView addSubview: _img_contentBackground];
    
    _msgContent = [self addElemContent];
    if (_msgContent) {
        [_img_contentBackground addSubview: _msgContent];
    }
}

// 添加群样式下的控件
- (void)addGroupCellViews {
    [self addC2CCellViews];
    
    _lab_nickName = [[UILabel alloc] init];
    _lab_nickName.font = [UIFont systemFontOfSize: 11];
    _lab_nickName.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview: _lab_nickName];
    
    _lab_remarkTips = [[UILabel alloc] init];
    _lab_remarkTips.font = [UIFont systemFontOfSize: 10];
    _lab_remarkTips.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview: _lab_remarkTips];
}

- (UIView*)addElemContent {
    return nil;
}


- (void)configWithMsg:(KKMessageItem*)msg {
    if (self.msg != msg) {
        self.msg = msg;
    }
    [self configContent];
}

//  配置消息内容
- (void)configContent {
    
}

@end
